package bid.yanhj.studyandriod.optionsmenu;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // xml添加
//        getMenuInflater().inflate(R.menu.main, menu);

        // 动态添加
        MenuItem mMenuItem = menu.add(1, 100, 1 , "菜单0");
        mMenuItem.setTitle("aaa");
        mMenuItem.setIcon(R.mipmap.ic_launcher);
        menu.add(1, 101, 1 , "菜单1");
        menu.add(1, 102, 1 , "菜单2");
        menu.add(1, 103, 1 , "菜单3");
        menu.add(1, 104, 1 , "菜单4");
        menu.add(1, 105, 1 , "菜单5");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.action_menu_item1:
                // 跳转意图
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                item.setIntent(intent);

                Toast.makeText(this, "点击菜单1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_menu_item2:
                Toast.makeText(this, "点击菜单2", Toast.LENGTH_SHORT).show();
                break;
            case 100:
                // 跳转意图
                Intent intent1 = new Intent(MainActivity.this, SecondActivity.class);
                item.setIntent(intent1);
                Toast.makeText(this, "点击菜单100", Toast.LENGTH_SHORT).show();
                break;
            case 101:
                Toast.makeText(this, "点击菜单101", Toast.LENGTH_SHORT).show();
                break;
            case 102:
                Toast.makeText(this, "点击菜单102", Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(this, "点击菜单", Toast.LENGTH_SHORT).show();
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}
